import api from '../../api';
import { message } from 'antd';

export const namespace = 'PortfolioManageLeverageAnalysisM';
export default {
  namespace,
  state: {},
  effects: {
    // 组合管理-保存标签设置
    *saveLabelSet({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioManageLeverageAnalysisApi.saveLabelSet, payload);
      if (response && response.code == '0') {
        message.success('标签设置成功');
        callback(true);
      } else {
        message.error(response?.msg || '保存失败！');
        callback(false);
      }
    },
    // 组合管理-获取标签设置数据
    *queryLabelSetData({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioManageLeverageAnalysisApi.getLabelSetData, payload);
      if (response && response.code == '0') {
        callback(response.data);
      } else {
        // callback([
        //   {
        //     productType: 1,
        //     highCriticalValue: 110,
        //     middleCriticalValue: 105,
        //     lowCriticalValue: 100,
        //   },
        //   {
        //     productType: 2,
        //     highCriticalValue: 120,
        //     middleCriticalValue: 110,
        //     lowCriticalValue: 100,
        //   },
        //   {
        //     productType: 3,
        //     highCriticalValue: 120,
        //     middleCriticalValue: 110,
        //     lowCriticalValue: 100,
        //   },
        // ]);
        callback([]);
        message.error(response?.msg || '获取标签数据失败！');
      }
    },
    // 组合管理-杠杆率趋势图获取
    *queryLeverageTrendChart({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioManageLeverageAnalysisApi.postLeverageTrendChart, payload);
      try {
        if (response?.code == '0') {
          callback(response.data);
        } else {
          // const mockdata = {
          //   code: '0',
          //   msg: 'xx',
          //   data: [
          //     {
          //       type: '手动阀手动阀地方',
          //       cordonValue: 190,
          //       data: [
          //         {
          //           20230301: 188,
          //           20230302: 66,
          //           20230303: 133,
          //           20230304: 99,
          //         },
          //       ],
          //     },
          //   ],
          // };
          // callback(mockdata.data);
          callback([]);
          message.error(response?.msg || '获取杠杆率趋势图失败！');
        }
      } catch (e) {
        console.error(e);
      }
    },
    // 组合管理-杠杆率明细结果表
    *queryLeverageDetailResults({ payload, callback }, { call, put }) {
      const response = yield call(api.portfolioManageLeverageAnalysisApi.leverageDetailResults, payload);
      let str = '';
      try {
        if (response?.code == '0') {
          const dataCopy = response.data;
          callback(
            response.data.dataSource.length === 0
              ? { dataSource: [null] }
              : { dataSource: dataCopy.dataSource.map((v) => ({ ...v, productValue: v.id })), pageInfo: dataCopy.pageInfo ?? { total: dataCopy.dataSource.length } },
          );
        } else {
          const noDataConfig = {
            dataSource: [null],
            pageInfo: { total: 0 },
          };
          if (payload.productType === 1) {
            // 现金管理类如果返回[], 用于判断产品初始化选中时候已经获取到了产品 所以接口返回为空数组时，应该处理成[null],
            str = '现金管理类';
            if (payload.sortType === 'ascend') {
              callback(noDataConfig);
              // callback({
              //   dataSource: [
              //     {
              //       id: '00465f0f1ad94c5e93c2b6560c08c6c0',
              //       productType: 1,
              //       productValue: '00465f0f1ad94c5e93c2b6560c08c6c0',
              //       productCode: 'BJ0727',
              //       productName: '自动化测试202205261044',
              //       maxLeverage: 99,
              //       maxLeverageDate: '2023-12-22',
              //       minLeverage: 23,
              //       minLeverageDate: '2022-03-22',
              //       averageLeverage: 84,
              //       differenceLever: 120,
              //     },
              //     {
              //       id: '003a36c135e141f3892262698d763485',
              //       productType: 1,
              //       productValue: '003a36c135e141f3892262698d763485',
              //       productCode: 'ZDH02',
              //       productName: '交易所回购簿记自动化',
              //       maxLeverage: 133,
              //       maxLeverageDate: '2022-12-22',
              //       minLeverage: 33,
              //       minLeverageDate: '2022-02-22',
              //       averageLeverage: 84,
              //       differenceLever: 100,
              //     },
              //   ],
              //   pageInfo: {
              //     total: 2,
              //   },
              // });
            } else {
              callback(noDataConfig);
              // callback({
              //   dataSource: [
              //     {
              //       id: '003a36c135e141f3892262698d763485',
              //       productType: 1,
              //       productValue: '003a36c135e141f3892262698d763485',
              //       productCode: 'ZDH02',
              //       productName: '交易所回购簿记自动化',
              //       maxLeverage: 133,
              //       maxLeverageDate: '2022-12-22',
              //       minLeverage: 33,
              //       minLeverageDate: '2022-02-22',
              //       averageLeverage: 84,
              //       differenceLever: 100,
              //     },
              //     {
              //       id: '00465f0f1ad94c5e93c2b6560c08c6c0',
              //       productType: 1,
              //       productValue: '00465f0f1ad94c5e93c2b6560c08c6c0',
              //       productCode: 'BJ0727',
              //       productName: '自动化测试202205261044',
              //       maxLeverage: 99,
              //       maxLeverageDate: '2023-12-22',
              //       minLeverage: 23,
              //       minLeverageDate: '2022-03-22',
              //       averageLeverage: 84,
              //       differenceLever: 120,
              //     },
              //   ],
              //   pageInfo: {
              //     total: 2,
              //   },
              // });
            }
          }
          if (payload.productType === 2) {
            str = '开放式公募';
            callback(noDataConfig);
            // callback({
            //   dataSource: [
            //     {
            //       id: '0080c47aff984f73908f0f46e330d229',
            //       productType: 2,
            //       productValue: '0080c47aff984f73908f0f46e330d229',
            //       productCode: 'TPD0211',
            //       productName: 'TPD0211',
            //       maxLeverage: 133,
            //       maxLeverageDate: '2022-12-22',
            //       minLeverage: 33,
            //       minLeverageDate: '2022-02-22',
            //       averageLeverage: 84,
            //       differenceLever: 100,
            //     },
            //     {
            //       id: '01528aff839b4ab0b57e416957703298',
            //       productType: 2,
            //       productValue: '01528aff839b4ab0b57e416957703298',
            //       productCode: 't02',
            //       productName: 't02',
            //       maxLeverage: 143,
            //       maxLeverageDate: '2023-12-22',
            //       minLeverage: 23,
            //       minLeverageDate: '2022-03-22',
            //       averageLeverage: 84,
            //       differenceLever: 120,
            //     },
            //   ],
            //   pageInfo: {
            //     total: 2,
            //   },
            // });
          }
          if (payload.productType === 3) {
            str = '封闭式及其他';
            const arr = [];
            for (let i = 0; i < 27; i++) {
              arr.push({
                id: `three${i + 1}`,
                productType: 3,
                productValue: `three${i + 1}`,
                productCode: `three${i + 1}`,
                productName: `three${i + 1}`,
                maxLeverage: 133,
                maxLeverageDate: '2022-12-22',
                minLeverage: 33,
                minLeverageDate: '2022-02-22',
                averageLeverage: 84,
                differenceLever: 100,
              });
            }
            callback(noDataConfig);
            // callback({
            //   dataSource: arr,
            //   pageInfo: {
            //     total: 27,
            //   },
            // });
          }
          message.error(response?.msg || `获取${str}列表失败！`);
        }
      } catch (e) {
        console.error(e);
      }
    },
  },
  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
